ids=[-1:-1:-6 1:7];
ord=[1 2 3 8 9 10 4 5 6 11 12 13 7];
[velP,velM,velP_ind,velM_ind,velP_raw,velM_raw]=get_vel_ind_from_adata(aux_data);

figure(101);
hold on

figure(102);
hold on;

for und=1:length(ids)
    psonsets=find(diff((round(2*aux_data(3,:)))==ids(und))==1);
    tmp_ons=[];
    for gnd=1:length(psonsets)
        cur_win=psonsets(gnd)-500:psonsets(gnd)+500;
        if sum(velM_ind(cur_win))/length(cur_win)==1
            tmp_ons(gnd)=psonsets(gnd);
        end
    end
    tmp_ons(tmp_ons==0)=[];
    fb1=[min(frame_times(1:20000)) max(frame_times(1:20000))];
    fb2=[min(frame_times(61001:81000)) max(frame_times(61001:81000))];
    psonsets=[tmp_ons(tmp_ons>fb1(1)&tmp_ons<fb1(2)) tmp_ons(tmp_ons>fb2(1)&tmp_ons<fb2(2))];

    ipsonsets=[];
    for ind=1:length(psonsets)
        [~,tmp]=min(abs(iframe_times-psonsets(ind)));
        ipsonsets(ind)=tmp;
    end
    
    throw_away=[];
    for vnd=1:length(nbr_iframes)-1
        throw_away=sum(nbr_iframes(1:vnd));
        ipsonsets(ismember(ipsonsets,throw_away-10:throw_away+10))=[];
    end
    figure(101);
    subplot(2,7,ord(und))
    for knd=1:length(ipsonsets)
        hold on
        plot(mean(pupil_pos(1,ipsonsets(knd)-4:ipsonsets(knd)-1)),mean(pupil_pos(2,ipsonsets(knd)-4:ipsonsets(knd)-1)),'ko')
    end
    for mnd=1:length(ipsonsets)
        hold on
        plot(mean(pupil_pos(1,ipsonsets(mnd)+27:ipsonsets(mnd)+30)),mean(pupil_pos(2,ipsonsets(mnd)+27:ipsonsets(mnd)+30)),'ro')
    end
    for ynd=1:length(ipsonsets)
        hold on
        line([mean(pupil_pos(1,ipsonsets(ynd)-4:ipsonsets(ynd)-1)) mean(pupil_pos(1,ipsonsets(ynd)+27:ipsonsets(ynd)+30))],[mean(pupil_pos(2,ipsonsets(ynd)-4:ipsonsets(ynd)-1)) mean(pupil_pos(2,ipsonsets(ynd)+27:ipsonsets(ynd)+30))],'color','black','linewidth',1.5)
    end
    xlim([90 130])
    ylim([100 180])
    
    figure(102);
    subplot(2,7,ord(und))
    ps_trig_sac=[];
    for xnd=1:length(ipsonsets)
        ps_trig_sac(:,xnd)=mean(pupil_pos(:,ipsonsets(xnd)+27:ipsonsets(xnd)+30),2)-mean(pupil_pos(:,ipsonsets(xnd)-4:ipsonsets(xnd)-1),2);
    end
    for bnd=1:size(ps_trig_sac,2)
        hold on
        plot(ps_trig_sac(1,bnd),ps_trig_sac(2,bnd),'o')
    end
    xlim(10*[-1 1])
    ylim(10*[-1 1])
end


ids=[-1:-1:-6 1:7];
ord=[1 2 3 8 9 10 4 5 6 11 12 13 7];
[velP,velM,velP_ind,velM_ind,velP_raw,velM_raw]=get_vel_ind_from_adata(aux_data);

figure(101);
hold on

figure(102);
hold on;

for und=1:length(ids)
    psonsets=find(diff((round(2*aux_data(3,:)))==ids(und))==1);
    tmp_ons=[];
    for gnd=1:length(psonsets)
        cur_win=psonsets(gnd)-500:psonsets(gnd)+1000;
        if sum(velM_ind(cur_win))==0
            if sum(velP_ind(cur_win))/length(cur_win)==1
                tmp_ons(gnd)=psonsets(gnd);
            end
        end
    end
    tmp_ons(tmp_ons==0)=[];
    
    pb1=[min(frame_times(20001:40500)) max(frame_times(20001:40500))];
    pb2=[min(frame_times(40501:61000)) max(frame_times(40501:61000))];
    pb3=[min(frame_times(81001:101500)) max(frame_times(81001:101500))];
    pb4=[min(frame_times(101501:122000)) max(frame_times(101501:122000))];
    psonsets=[tmp_ons(tmp_ons>pb1(1)&tmp_ons<pb1(2)) tmp_ons(tmp_ons>pb2(1)&tmp_ons<pb2(2)) tmp_ons(tmp_ons>pb3(1)&tmp_ons<pb3(2)) tmp_ons(tmp_ons>pb4(1)&tmp_ons<pb4(2))];
%     psonsets=[tmp_ons(tmp_ons>pb1(1)&tmp_ons<pb1(2)) tmp_ons(tmp_ons>pb2(1)&tmp_ons<pb2(2))];
%     psonsets=[tmp_ons(tmp_ons>pb3(1)&tmp_ons<pb3(2)) tmp_ons(tmp_ons>pb4(1)&tmp_ons<pb4(2))];

    ipsonsets=[];
    for ind=1:length(psonsets)
        [~,tmp]=min(abs(iframe_times-psonsets(ind)));
        ipsonsets(ind)=tmp;
    end
    
    throw_away=[];
    for vnd=1:length(nbr_iframes)-1
        throw_away=sum(nbr_iframes(1:vnd));
        ipsonsets(ismember(ipsonsets,throw_away-10:throw_away+10))=[];
    end
    figure(101);
    subplot(2,7,ord(und))
    for knd=1:length(ipsonsets)
        hold on
        plot(mean(pupil_pos(1,ipsonsets(knd)-4:ipsonsets(knd)-1)),mean(pupil_pos(2,ipsonsets(knd)-4:ipsonsets(knd)-1)),'ko')
    end
    for mnd=1:length(ipsonsets)
        hold on
        plot(mean(pupil_pos(1,ipsonsets(mnd)+27:ipsonsets(mnd)+30)),mean(pupil_pos(2,ipsonsets(mnd)+27:ipsonsets(mnd)+30)),'ro')
    end
    for ynd=1:length(ipsonsets)
        hold on
        line([mean(pupil_pos(1,ipsonsets(ynd)-4:ipsonsets(ynd)-1)) mean(pupil_pos(1,ipsonsets(ynd)+27:ipsonsets(ynd)+30))],[mean(pupil_pos(2,ipsonsets(ynd)-4:ipsonsets(ynd)-1)) mean(pupil_pos(2,ipsonsets(ynd)+27:ipsonsets(ynd)+30))],'color','black','linewidth',1.5)
    end
    xlim([60 160])
    ylim([70 180])
    
    figure(102);
    subplot(2,7,ord(und))
    ps_trig_sac=[];
    for xnd=1:length(ipsonsets)
        ps_trig_sac(:,xnd)=mean(pupil_pos(:,ipsonsets(xnd)+27:ipsonsets(xnd)+30),2)-mean(pupil_pos(:,ipsonsets(xnd)-4:ipsonsets(xnd)-1),2);
    end
    for bnd=1:size(ps_trig_sac,2)
        hold on
        plot(ps_trig_sac(1,bnd),ps_trig_sac(2,bnd),'o')
    end
    xlim(10*[-1 1])
    ylim(10*[-1 1])
end



